
package com.wyzz.hr.bo.impl;

import java.util.List;
import org.apache.axis.utils.StringUtils;
import org.hibernate.criterion.*;

import com.wyzz.hr.base.ComonBeans;
import com.wyzz.hr.bo.IRecruitappcontactBo;
import com.wyzz.hr.bo.ISysLogBO;
import com.wyzz.hr.dao.IRecruitappcontactDAO;
import com.wyzz.hr.pojo.Employee;
import com.wyzz.hr.pojo.Recruitappcontact;
import com.wyzz.hr.pojo.Recruitapplier;
import com.wyzz.hr.pojo.Recruitplan;
import com.wyzz.hr.spring.SpringBeanFactory;
import com.wyzz.hr.util.BaseCrit;
import com.wyzz.hr.util.DateUtil;
import com.wyzz.hr.util.StringUtil;

public class RecruitappcontactBoImpl implements IRecruitappcontactBo {

	public RecruitappcontactBoImpl() {
		msgOper0 = "\u9762\u8BD5";
		msgOper1 = "HR\u8054\u7CFB";
		msgComment0 = "{0}{1}:{2}";
		msgComment1 = "\u8054\u7CFB\u8005:{0},\u8BC4\u4EF7:{1}";
	}

	public Recruitappcontact loadAppContact(Object id, String fetch[]) {
		return (Recruitappcontact) appcontactDAO.loadObject(
				com.wyzz.hr.pojo.Recruitappcontact.class, id, fetch,
				new boolean[0]);
	}

	public List getContactListByApp(String applierId, String catNo) {
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Recruitappcontact.class);
		dc.createAlias(Recruitappcontact.PROP_RECAC_RECA_ID, "recaId", 1);
		dc.createAlias((new StringBuilder()).append("recaId.").append(
				Recruitapplier.PROP_RECA_INTERVIEWER).toString(),
				"recaInterviewer", 1);
		dc.createAlias((new StringBuilder()).append(
				Recruitappcontact.PROP_RECAC_RECP_ID).append(".").append(
				Recruitplan.PROP_RECP_PB_NO).toString(), "pbNo", 1);
		dc.createAlias(Recruitappcontact.PROP_RECAC_CREATE_BY, "recacCreateBy",
				1);
		dc.createAlias((new StringBuilder()).append("recacCreateBy.").append(
				Employee.PROP_EMP_DEPT_NO).toString(),
				Employee.PROP_EMP_DEPT_NO, 1);
		dc.createAlias((new StringBuilder()).append("recacCreateBy.").append(
				Employee.PROP_EMP_LOCATION_NO).toString(),
				Employee.PROP_EMP_LOCATION_NO, 1);
		dc.add(Restrictions.eq((new StringBuilder()).append(
				Recruitappcontact.PROP_RECAC_RECA_ID).append(".").append(
				Recruitapplier.PROP_ID).toString(), applierId));
		if (catNo != null)
			dc.add(Restrictions.eq(Recruitappcontact.PROP_RECAC_CAT_NO, Integer
					.valueOf(Integer.parseInt(catNo))));
		dc.addOrder(Order.desc(Recruitappcontact.PROP_RECAC_LAST_CHANGE_TIEM));
		return appcontactDAO.findByCriteria(dc);
	}

	public boolean addAppcontact(Recruitappcontact contact, Employee currentEmp) {
		try {
			appcontactDAO.saveObject(contact);
			if (contact.getRecacCatNo().intValue() == 1
					&& contact.getRecacRecaId().getRecaStatus().intValue() == 11) {
				if (!StringUtils.isEmpty(contact.getInterviewerId())) {
					String hql = (new StringBuilder()).append(
							"update Recruitapplier set recaInterviewer='")
							.append(contact.getInterviewerId()).append("' ")
							.append("where id='").append(
									contact.getRecacRecaId().getId()).append(
									"'").toString();
					appcontactDAO.exeHql(hql);
				}
				if (contact.getInterviewTime() != null) {
					String dateStr = DateUtil.formatDateToS(contact
							.getInterviewTime(), "yyyy-MM-dd HH:mm");
					String hql = (new StringBuilder()).append(
							"update Recruitapplier set recaInterviewTime='")
							.append(dateStr).append("' where id='").append(
									contact.getRecacRecaId().getId()).append(
									"'").toString();
					appcontactDAO.exeHql(hql);
				}
			}
			String action = null;
			String comment = null;
			if (contact.getRecacCatNo().intValue() == 0) {
				action = "\u9762\u8BD5";
				comment = StringUtil.message(msgComment0, new Object[] {
						contact.getRecacEmpName(),
						contact.getRecacType(),
						ComonBeans.getParameterValue("recaEmpRate",
								new String[] { (new StringBuilder()).append(
										contact.getRecacRate()).append("")
										.toString() }) });
				ISysLogBO logBO = (ISysLogBO) SpringBeanFactory
						.getBean("logBO");
				logBO.addToSyslog("recruitapplier", currentEmp.getId(), null,
						contact.getRecacRecaId().getId(), 0, action, comment);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

	public boolean updateAppcontact(Recruitappcontact contact,
			Employee currentEmp) {
		try {
			appcontactDAO.saveOrupdate(contact);
			if (contact.getRecacCatNo().intValue() == 1
					&& contact.getRecacRecaId().getRecaStatus().intValue() == 11) {
				if (!StringUtils.isEmpty(contact.getInterviewerId())) {
					String hql = (new StringBuilder()).append(
							"update Recruitapplier set recaInterviewer='")
							.append(contact.getInterviewerId()).append("' ")
							.append("where id='").append(
									contact.getRecacRecaId().getId()).append(
									"'").toString();
					appcontactDAO.exeHql(hql);
				}
				if (contact.getInterviewTime() != null) {
					String dateStr = DateUtil.formatDateToS(contact
							.getInterviewTime(), "yyyy-MM-dd HH:mm");
					String hql = (new StringBuilder()).append(
							"update Recruitapplier set recaInterviewTime='")
							.append(dateStr).append("' where id='").append(
									contact.getRecacRecaId().getId()).append(
									"'").toString();
					appcontactDAO.exeHql(hql);
				}
			}
			String action = null;
			String comment = null;
			if (contact.getRecacCatNo().intValue() == 0) {
				action = "\u4FEE\u6539\u9762\u8BD5";
				comment = StringUtil.message(msgComment0, new Object[] {
						contact.getRecacEmpName(),
						contact.getRecacType(),
						ComonBeans.getParameterValue("recaEmpRate",
								new String[] { (new StringBuilder()).append(
										contact.getRecacRate()).append("")
										.toString() }) });
				ISysLogBO logBO = (ISysLogBO) SpringBeanFactory
						.getBean("logBO");
				logBO.addToSyslog("recruitapplier", currentEmp.getId(), null,
						contact.getRecacRecaId().getId(), 0, action, comment);
			}
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

	public boolean deleteAppcontact(String id) {
		String hql = (new StringBuilder()).append(
				"delete from Recruitappcontact where id = '").append(id)
				.append("'").toString();
		try {
			appcontactDAO.exeHql(hql);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

	public List loadAppcontactByIds(String ids[]) {
		if (ids == null || ids.length == 0)
			return null;
		DetachedCriteria dc = DetachedCriteria
				.forClass(com.wyzz.hr.pojo.Recruitappcontact.class);
		dc.createAlias(Recruitappcontact.PROP_RECAC_RECA_ID, "recacRecaId", 1);
		BaseCrit.addDC(dc, Recruitappcontact.PROP_RECAC_CAT_NO, "eq",
				new Integer[] { new Integer(0) });
		BaseCrit.addDC(dc, (new StringBuilder()).append(
				Recruitappcontact.PROP_RECAC_RECA_ID).append(".id").toString(),
				"in", ids);
		dc.addOrder(Order.asc(Recruitappcontact.PROP_RECAC_LAST_CHANGE_TIEM));
		List result = appcontactDAO.findByCriteria(dc);
		if (result.isEmpty())
			return null;
		else
			return result;
	}

	public IRecruitappcontactDAO getAppcontactDAO() {
		return appcontactDAO;
	}

	public void setAppcontactDAO(IRecruitappcontactDAO appcontactDAO) {
		this.appcontactDAO = appcontactDAO;
	}

	String msgOper0;
	String msgOper1;
	String msgComment0;
	String msgComment1;
	private IRecruitappcontactDAO appcontactDAO;
}
